export default {

	getCurrentDateTimestring() {
		// return new Date().toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	getCurrenTimestamp() {
		return Date.now() / 1000;
	},


	stringToTimestamp(dateStr) {
		const date = new Date(dateStr);
		return Math.floor(date.getTime() / 1000);
	},
	stringToDatetime(dateStr) {
		return new Date(dateStr);
	},
	timestampToString(timestamp) {
		const datetime = new Date(timestamp*1000);
		return this.toISOString(datetime);
	},
	timestampToDatetime(timestamp) {
		return new Date(timestamp*1000);
	},
	datetimeToTimestamp(datetime) {
		return Math.floor(datetime.getTime() / 1000);
	},
	datetimeToString(datetime) {
		return this.toISOString(datetime);
	},



	getYear(dateStr) {
		return new Date(dateStr).getFullYear();
	},

	getMonth(dateStr) {
		return new Date(dateStr).getMonth() + 1;
	},

	getMonthDay(dateStr) {
		return new Date(dateStr).getDate();
	},

	getWeek(dateStr) {
		let date = new Date(dateStr);
		let week = date.toLocaleString('en-US', {
			week: 'numeric'
		});
		return parseInt(week, 10);
	},
	getWeekday(dateStr) {
		let date = new Date(dateStr);
		let weekday = date.toLocaleString('en-US', {
			weekday: 'long'
		});
		// Map the weekday string to a number (1-7) where 1 is Monday and 7 is Sunday
		const weekdayNumbers = {
			'Monday': 1,
			'Tuesday': 2,
			'Wednesday': 3,
			'Thursday': 4,
			'Friday': 5,
			'Saturday': 6,
			'Sunday': 7
		};
		return weekdayNumbers[weekday];
	},

	getHour(dateStr) {
		return new Date(dateStr).getHours();
	},

	getMinute(dateStr) {
		return new Date(dateStr).getMinutes();
	},

	getSecond(dateStr) {
		return new Date(dateStr).getSeconds();
	},


	parse(dateStr) {
		const patterns = [
			'yyyy-MM-dd HH:mm:ss', // 2024-01-01 00:00:00
			'yyyy-MM-dd HH:mm', // 2024-01-01 12:00
			'yyyy/MM/dd HH:mm', // 2024/01/01 00:00
			'yyyy/MM/dd HH:mm:ss' // 2024/01/01 00:00:00
		];
		let date;
		for (let pattern of patterns) {
			date = new Date(dateStr);
			if (!isNaN(date.getTime())) {
				return date;
			}
		}
		throw new Error(`Invalid date format: ${dateStr}`);
	},

	format(date, pattern = 'yyyy-MM-dd HH:mm:ss') {
		if (!(date instanceof Date)) {
			throw new Error('The provided object is not a Date instance.');
		}
		const replacer = {
			'Y': date.getFullYear(),
			'm': date.getMonth() + 1,
			'd': date.getDate(),
			'H': date.getHours(),
			'i': date.getMinutes(),
			's': date.getSeconds()
		};
		return pattern.replace(/Y{4}|m{2}|d{2}|H{2}|i{2}|s{2}/g, match => replacer[match]);
	},

	addDays(dateStr, amount) {
		const date = new Date(dateStr);
		date.setDate(date.getDate() + amount);
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	addHours(dateStr, amount) {
		const date = new Date(dateStr);
		date.setHours(date.getHours() + amount);
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	addWeeks(dateStr, amount) {
		const date = new Date(dateStr);
		date.setDate(date.getDate() + (amount * 7));
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	addMonths(dateStr, amount) {
		const date = new Date(dateStr);
		const newMonth = date.getMonth() + amount;
		date.setMonth(newMonth);
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	addYears(dateStr, amount) {
		const date = new Date(dateStr);
		date.setFullYear(date.getFullYear() + amount);
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	subDatetimeYears(beginDateStr, endDateStr) {
		const beginDate = new Date(beginDateStr);
		const endDate = new Date(endDateStr);
		return Math.floor(endDate.getFullYear() - beginDate.getFullYear());
	},

	subDatetimeMonths(beginDateStr, endDateStr) {
		const beginDate = new Date(beginDateStr);
		const endDate = new Date(endDateStr);
		const yearsDiff = endDate.getFullYear() - beginDate.getFullYear();
		const monthsDiff = endDate.getMonth() - beginDate.getMonth();
		return Math.floor(yearsDiff * 12 + monthsDiff);
	},

	subDatetimeDays(beginDateStr, endDateStr) {
		const beginDate = new Date(beginDateStr);
		const endDate = new Date(endDateStr);
		return Math.floor((endDate - beginDate) / (1000 * 60 * 60 * 24));
	},

	subDatetimeHours(beginDateStr, endDateStr) {
		const beginDate = new Date(beginDateStr);
		const endDate = new Date(endDateStr);
		return Math.floor((endDate - beginDate) / (1000 * 60 * 60)); //0.9天算0天
	},

	subDatetimeMinutes(beginDateStr, endDateStr) {
		const beginDate = new Date(beginDateStr);
		const endDate = new Date(endDateStr);
		return Math.floor((endDate - beginDate) / (1000 * 60));
	},

	subDatetimeSeconds(beginDateStr, endDateStr) {
		const beginDate = new Date(beginDateStr);
		const endDate = new Date(endDateStr);
		return Math.floor(endDate - beginDate) / 1000;
	},

	truncateHour(dateStr) {
		const date = new Date(dateStr);
		date.setMinutes(0);
		date.setSeconds(0);
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},

	truncateMinute(dateStr) {
		const date = new Date(dateStr);
		date.setSeconds(0);
		// return date.toISOString().replace('T', ' ').substring(0, 19);
		return this.toISOString(date);
	},



	toISOString(d) {
		var z = n => ('0' + n).slice(-2);
		var zz = n => ('00' + n).slice(-3);
		var off = d.getTimezoneOffset();
		var sign = off > 0 ? '-' : '+';
		off = Math.abs(off);

		return d.getFullYear() + '-' +
			z(d.getMonth() + 1) + '-' +
			z(d.getDate()) + ' ' +
			z(d.getHours()) + ':' +
			z(d.getMinutes()) + ':' +
			z(d.getSeconds());
	}

}